package com.wxtl.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.wxtl.action.qo.PackQo;
import com.wxtl.dao.base.HibernateBaseDao;
import com.wxtl.entity.UpgradePack;
import com.wxtl.util.DataGridModel;
import com.wxtl.util.DateUtil;
import com.wxtl.util.Page;
import com.wxtl.util.Status;

/**
 * 升级补丁Dao
 * @author hc24
 *
 */
@Repository
public class UpgradePackDAO extends HibernateBaseDao {
	private static final Log log = LogFactory.getLog(UpgradePackDAO.class);

	/**
	 * 根据产品编号、版本号、修正号查询
	 * @param upList
	 * @return
	 */
	public List<UpgradePack> queryPackList(List<UpgradePack> upList){
		StringBuilder hql=new StringBuilder();
		List<String> params=new ArrayList<String>();
		
		hql.append("from UpgradePack up where 1=1");
		if(upList!=null&&upList.size()>0){
			hql.append(" and");
			for(int i=0;i<upList.size();i++){
				if(i==0){
					hql.append("(");
				}else{
					hql.append(" or");
				}
				hql.append(" up.proCode=?");
				params.add(upList.get(i).getProCode());
				
				if(i==upList.size()-1){
					hql.append(")");
				}
			}
		}
		Query query= getSession().createQuery(hql.toString());
		for(int i=0;i<params.size();i++){
			query.setString(i, params.get(i));
		}
		List<UpgradePack> list=query.list();
		
		List<UpgradePack> newList=new ArrayList<UpgradePack>();
		
		//过滤版本号  这个地方好绕
		for(int i=0;i<list.size();i++){
			UpgradePack up=list.get(i);
			String version=up.getSuptVersion();
			String[] strs=version.split(",");
			float sub=0;
			float sup=0;
			String strSub=strs[0];
			String strSup=strs[1];
			if("+".equals(strSup)){
				sub=Float.parseFloat(strSub);
			}else if("-".equals(strSup)){
				sup=Float.parseFloat(strSup);
			}else{
				sub=Float.parseFloat(strSub);
				sup=Float.parseFloat(strSup);
			}
			for(int j=0;j<upList.size();j++){
				UpgradePack tempUp=upList.get(j);
				if(!tempUp.getProCode().equals(up.getProCode()))//必须是当前产品编码
					continue;
				float ver=Float.parseFloat(tempUp.getSuptVersion());
				if(sub>0&&sup>0){
					if(ver>=sub&&ver<=sup){//满足版本过滤条件
						newList.add(up);
					}
				}else if(sub>0&&sup==0){//满足版本过滤条件
					if(ver>=sub){
						newList.add(up);
					}
				}else if(sub==0&&sup>0){//满足版本过滤条件
					if(ver<=sup){
						newList.add(up);
					}
				}
			}
		}
		
		
		List<UpgradePack> newnewList=new ArrayList<UpgradePack>();
		//过滤修正号  这个地方好绕
		for(int i=0;i<newList.size();i++){
			UpgradePack up=newList.get(i);
			String rversion=up.getSuptRevision();
			String[] strs=rversion.split(",");
			float sub=0;
			float sup=0;
			String strSub=strs[0];
			String strSup=strs[1];
			if("+".equals(strSup)){
				sub=Float.parseFloat(strSub);
			}else if("-".equals(strSup)){
				sup=Float.parseFloat(strSup);
			}else{
				sub=Float.parseFloat(strSub);
				sup=Float.parseFloat(strSup);
			}
			for(int j=0;j<upList.size();j++){
				UpgradePack tempUp=upList.get(j);
				if(!tempUp.getProCode().equals(up.getProCode()))//必须是当前产品编码
					continue;
				float rver=Float.parseFloat(tempUp.getSuptRevision());
				if(sub>0&&sup>0){
					if(rver>=sub&&rver<=sup){//满足版本过滤条件
						newnewList.add(up);
					}
				}else if(sub>0&&sup==0){//满足版本过滤条件
					if(rver>=sub){
						newnewList.add(up);
					}
				}else if(sub==0&&sup>0){//满足版本过滤条件
					if(rver<=sup){
						newnewList.add(up);
					}
				}
			}
		}
		
		return newnewList;
	}
	
	/**
	 * 根据升级包编码查询
	 * @param upcode 升级包编码
	 * @return
	 */
	public UpgradePack queryPackByUpcode(String upcode){
		String hql="from UpgradePack p where p.upCode=?";
		List<UpgradePack> list=getSession().createQuery(hql).setString(0, upcode).list();
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 根据UpgradePack查询条件查询
	 * @param dgm
	 * @param pack
	 * @return
	 */
	public Page query(DataGridModel dgm,UpgradePack pack){
		//构建hql
		StringBuilder hql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		hql.append("from UpgradePack p where 1=1 ");
		if(pack.getUpName()!=null&&!"".equals(pack.getUpName())){
			params.add("%"+pack.getUpName()+"%");
			hql.append(" and p.upName like ?");
		}
		if(pack.getUpCode()!=null&&!"".equals(pack.getUpCode())){
			params.add("%"+pack.getUpCode()+"%");
			hql.append(" and p.upCode like ?");
		}
		
		if(pack.getProCode()!=null&&!"".equals(pack.getProCode())){
			params.add("%"+pack.getProCode()+"%");
			hql.append(" and p.proCode like ?");
		}
		
		if(pack.getUpType()!=null){
			params.add(pack.getUpType());
			hql.append(" and p.upType=?");
		}
		
		if(pack.getState()!=null){
			params.add(pack.getState());
			hql.append(" and p.state=?");
		}
		
		hql.append(" order by p.releaseDate desc, p.upCode");
		
		return super.queryForList(hql.toString(), params.toArray(new Object[]{}), dgm.getPage(), dgm.getRows());
	}
	
	/**
	 * 根据packQo查询条件查询
	 * @param dgm
	 * @param packQo
	 * @return
	 */
	public Page query(DataGridModel dgm,PackQo packQo){
		//构建hql
		StringBuilder hql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		hql.append("from UpgradePack p where 1=1 ");
		if(packQo.getUpCode()!=null&&!"".equals(packQo.getUpCode())){
			params.add("%"+packQo.getUpCode()+"%");
			hql.append(" and p.upCode like ?");
		}
		
		if(packQo.getProCode()!=null&&!"".equals(packQo.getProCode())){
			params.add("%"+packQo.getProCode()+"%");
			hql.append(" and p.proCode like ?");
		}
		if(packQo.getVersion()!=null&&!"".equals(packQo.getVersion())){
			params.add(packQo.getVersion());
			hql.append(" and p.exVersion=?");
		}
		if(packQo.getCreateBeginDate()!=null){
			params.add(packQo.getCreateBeginDate());
			hql.append(" and p.releaseDate>=?");
		}
		if(packQo.getCreateEndDate()!=null){
			params.add(DateUtil.next(packQo.getCreateEndDate(),1));
			hql.append(" and p.releaseDate<=?");
		}
		params.add(Status.STATE_DELETE);
		hql.append(" and p.state!=?");
		
		hql.append(" order by p.releaseDate desc, p.upCode");
		
		return super.queryForList(hql.toString(), params.toArray(new Object[]{}), dgm.getPage(), dgm.getRows());
	}
	
	public UpgradePack load(int upgradeId){
		return (UpgradePack) getHibernateTemplate().load(UpgradePack.class, upgradeId);
	}
	
	public void delete(int[] ids){
		//super.delete("upgrade_pack","upgradeId", ids);
		super.deleteByMark("upgrade_pack", "upgradeId", ids);
	}
	
	public void deleteByProCode(String[] codes){
		//super.delete("upgrade_pack","upgradeId", ids);
		super.deleteByMark("upgrade_pack", "proCode", codes);
	}
	
	public UpgradePack getUpgradePackById(int id){
		return (UpgradePack) getHibernateTemplate().get(UpgradePack.class, id);
	}
}